public class CampaignMbrStatus {

	Public static boolean CheckCMStatusExistsBulk (set<id> CampIDs, string Status, boolean requireResponded ) {
		
		map <id,CampaignMemberStatus> CampStatusMap = new Map <id,CampaignMemberStatus> ();
		boolean isSuccess = false;
		list<CampaignMemberStatus> CMSToUpdate = new list<CampaignMemberStatus> ();
		list<CampaignMemberStatus> CMSToInsert = new list<CampaignMemberStatus> ();
		
		// query all CMStatus where status matches and campid is in the list
		CampaignMemberStatus[] CMSFound = [Select CampaignId, Label, HasResponded From CampaignMemberStatus WHERE Label = :Status AND CampaignId IN :CampIDs ];
		
		// add to map <campId,cmstatus obj>
		for ( CampaignMemberStatus thisCMS : CMSFound ) {
			CampStatusMap.put (	thisCMS.CampaignId , thisCMS );
		}
				
		// loop through original list of campid's
		for ( id CampId : CampIDs ) {
			
			CampaignMemberStatus thisCMS = CampStatusMap.get(CampId);
			
			if ( thisCMS != null ) {
				// if in map, see if existing one is responded (if required)
				// (if so, do nothing)
				if ( thisCMS.HasResponded == false && requireResponded == true ) {
					// if not, chg it so it is (add to list of changes)
					thisCMS.HasResponded = true;
					CMSToUpdate.add (thisCMS);	
				}
			} else {
				// if not in map, status doesn't exist, so add to list of inserts
				// TBD first have to look up what the SortOrder should be for this new status
				// should make it last
				// for now, just set it to a high number
				
				CampaignMemberStatus newCMS = new CampaignMemberStatus (
					CampaignId = CampId,
					Label = Status,
					HasResponded = requireResponded,
					SortOrder = 99
				);
				CMSToInsert.add (newCMS);
				
			}
		}
		
		// do inserts
		if ( CMSToInsert.size () > 0 ) {
			insert CMSToInsert;	
		}
		
		// do updates
		if ( CMSToUpdate.size () > 0 ) {
			update CMSToUpdate;	
		}
		
		isSuccess = true;
		
		return isSuccess;
	
	}


	Public Static boolean CheckCMStatusExists (Id campId, string CMStatus, boolean isResponded ) {
	
		//original method, one rec only
	
		// for a given Campaign, look to see if a CM Status w/ given name exists
		// if not, create it
		// also require it to be IsResponded if that param is set, chg that if necessary(!!)
	
		boolean statusExists;
		
		CampaignMemberStatus[] cStatus = [Select c.Label, c.HasResponded From CampaignMemberStatus c WHERE c.CampaignId = :campId AND c.Label = :CMStatus ];			
		
		If ( cStatus.size () > 0 ) {
			// if we found a rec (should only return 1 rec, if any)
			// then require it to be Responded if necessary
			
			if (isResponded == True && cStatus[0].HasResponded != True ) {
				// chg status to HasResponded	
			}
			
			statusExists = True;
			
		} else {
			
			// if status doesn't exist, create it
			
			// TBD first have to look up what the SortOrder should be for this new status
			// should make it last
			// for now, just set it to a high number
			
			CampaignMemberStatus newCMStatus = new CampaignMemberStatus (
				Label = CMStatus,
				CampaignId = campId,
				HasResponded = isResponded,
				SortOrder = 99
			);
			
			insert newCMStatus;
			
			statusExists = True;
		}
		
		return statusExists;	
	
	}


	Public Static string GetRespondedStatus (Id campId) {
	
		// for a given campaign, look up the 'default' status to use for Responded
		// not used currently, may be able to delete
		
		string s;
	
		// first get list of all the statuses w/ HasResponded (should never be empty set)
		CampaignMemberStatus[] cStatus = [Select c.Label, c.IsDefault From CampaignMemberStatus c WHERE c.CampaignId = :campId And c.HasResponded = True ORDER BY c.IsDefault desc];	

		If ( cStatus[0].IsDefault == True ) {
			//if there's a default status that's Responded, use that
			s = cStatus[0].Label;
		} Else {
			// else use 'Responded', if it exists for this camp.
			For ( CampaignMemberStatus eachStatus : cStatus ) {
				If ( eachStatus.Label == 'Responded' ) s = eachStatus.Label;
			}	
			// otherwise, just use the first one queried, at random
			If ( s == Null ) s = cStatus[0].Label;
		}
		
		return s;
	}

}